/**
 * 产品管理管理初始化
 */
var ProductFx = {
    id: "ProductFxTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
ProductFx.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
            {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
            {title: 'Entry Date', field: 'entryDate', visible: true, align: 'center', valign: 'middle', width: '100px', formatter: function (value, row, index) {
                    return DateUtils.changeDateFormat(value);
                }},
            {title: 'Deal No.', field: 'dealNo', visible: true, align: 'center', valign: 'middle', width: '100px'},
            {title: 'Trade Date', field: 'tradeDate', visible: true, align: 'center', valign: 'middle', width: '100px', formatter: function (value, row, index) {
                    return DateUtils.changeDateFormat(value);
                }},
            {title: 'Customer', field: 'customer', visible: true, align: 'center', valign: 'middle', width: '100px'},
            {title: 'Buy/Sell', field: 'buySell', visible: true, align: 'center', valign: 'middle', width: '100px'},
            {title: 'Currency 1', field: 'currency1', visible: true, align: 'center', valign: 'middle', width: '100px'},
            {title: 'Notional', field: 'notional', visible: true, align: 'center', valign: 'middle', width: '100px'},
            {title: 'Currency 2', field: 'currency2', visible: true, align: 'center', valign: 'middle', width: '100px'},
            {title: 'Counter value', field: 'counterValue', visible: true, align: 'center', valign: 'middle', width: '100px'},
            {title: 'Strike', field: 'strike', visible: true, align: 'center', valign: 'middle', width: '100px'},
            {title: 'Value Date', field: 'valueDate', visible: true, align: 'center', valign: 'middle', width: '100px', formatter: function (value, row, index) {
                    return DateUtils.changeDateFormat(value);
                }},
            {title: 'Product Level 1', field: 'productLevel1', visible: true, align: 'center', valign: 'middle', width: '100px'},
            {title: 'Product Level 2', field: 'productLevel2', visible: true, align: 'center', valign: 'middle', width: '100px'},
            {title: 'Product Level 3', field: 'productLevel3', visible: true, align: 'center', valign: 'middle', width: '100px'},
            {title: 'Premium Ccy', field: 'premiumCcy', visible: true, align: 'center', valign: 'middle', width: '100px'},
            {title: 'Premium Amount', field: 'premiumAmount', visible: true, align: 'center', valign: 'middle', width: '100px'},
            {title: 'Expiry Date', field: 'expiryDate', visible: true, align: 'center', valign: 'middle', width: '100px', formatter: function (value, row, index) {
                    return DateUtils.changeDateFormat(value);
                }},
            {title: 'Delivery Date', field: 'deliveryDate', visible: true, align: 'center', valign: 'middle', width: '100px', formatter: function (value, row, index) {
                    return DateUtils.changeDateFormat(value);
                }},
            {title: 'Counter Party', field: 'counterParty', visible: true, align: 'center', valign: 'middle', width: '100px'},
            {title: 'Retrocession in Ccy 1', field: 'retrocessionInCcy1', visible: true, align: 'center', valign: 'middle', width: '100px'},
            {title: 'Retrocession in USD', field: 'retrocessionInUsd', visible: true, align: 'center', valign: 'middle', width: '100px'},
            {title: 'Traded By', field: 'tradedBy', visible: true, align: 'center', valign: 'middle', width: '100px'},
            {title: 'Entry Time', field: 'entryTime', visible: true, align: 'center', valign: 'middle', width: '100px'},
            {title: 'Position Status', field: 'positionStatus', visible: true, align: 'center', valign: 'middle', width: '100px'},
            {title: 'Strategy Code', field: 'strategyCode', visible: true, align: 'center', valign: 'middle', width: '100px'},
            {title: 'Classification', field: 'classification', visible: true, align: 'center', valign: 'middle', width: '100px'},
            {title: 'Gamma/Vega add-on', field: 'gammaVega', visible: true, align: 'center', valign: 'middle', width: '100px'},
            {title: 'Ccy2 Trade', field: 'ccyTrade', visible: true, align: 'center', valign: 'middle', width: '100px'},
            {title: 'Squared Options', field: 'squaredOptions', visible: true, align: 'center', valign: 'middle', width: '100px'},
            {title: 'X-Curncy Premium (USD)', field: 'curncyPremium', visible: true, align: 'center', valign: 'middle', width: '100px'},
            {title: 'Input Time', field: 'inputTime', visible: true, align: 'center', valign: 'middle', width: '100px'}
    ];
};

/**
 * 检查是否选中
 */
ProductFx.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        ProductFx.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加产品管理
 */
ProductFx.openAddProductFx = function () {
    var index = layer.open({
        type: 2,
        title: '添加产品管理',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/productFx/productFx_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看产品管理详情
 */
ProductFx.openProductFxDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '产品管理详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/productFx/productFx_update/' + ProductFx.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除产品管理
 */
ProductFx.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/productFx/delete", function (data) {
            Feng.success("删除成功!");
            ProductFx.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("productFxId",this.seItem.id);
        ajax.start();
    }
};

/**
 * 查询产品管理列表
 */
ProductFx.search = function () {
    var queryData = {};
    queryData['customer'] = $("#customer").val();
    queryData['entryDate'] = $("#entryDate").val();
    queryData['tradeDate'] = $("#tradeDate").val();
    ProductFx.table.refresh({query: queryData});
};

/**
 * Closed客户购买FX产品
 */
ProductFx.closedFx = function () {
    if(this.check()){
        var ajax = new $ax(Feng.ctxPath + "/productFx/closed", function (data) {
            Feng.success("Closed成功!");
            ProductFx.table.refresh();
        }, function (data) {
            Feng.error("Closed失败!" + data.responseJSON.message + "!");
        });
        ajax.set("productFxId",this.seItem.id);
        ajax.start();
    }
}

/**
 * Open客户购买FX产品
 */
ProductFx.openFx = function () {
    if(this.check()){
        var ajax = new $ax(Feng.ctxPath + "/productFx/open", function (data) {
            Feng.success("Open成功!");
            ProductFx.table.refresh();
        }, function (data) {
            Feng.error("Open失败!" + data.responseJSON.message + "!");
        });
        ajax.set("productFxId",this.seItem.id);
        ajax.start();
    }
}

$(function () {
    var defaultColunms = ProductFx.initColumn();
    var table = new BSTable(ProductFx.id, "/productFx/list", defaultColunms);
    table.setPaginationType("client");
    ProductFx.table = table.init();
});
